import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import LayOut from "../views/layout/LayOut.vue";
import { $localStorage } from "@/utils/utils";
// parent字段是二级菜单表示，children字段是三级菜单表示
// level字段三级菜单的层级，menuName字段三级菜单的标题level对应的是要放在那个一级菜单下
const routes = [
  {
    path: "/",
    redirect: "/layout/home",
  },
  {
    path: "/layout",
    name: "layout",
    component: LayOut,
    redirect: "/layout/home",
    children: [
      {
        path: "home",
        name: "home",
        component: HomeView,
        meta: {
          title: "home页面",
          //相同的parent会在菜单中显示在同一层级
          // parent: "菜单项1",
        },
      },
      {
        path: "about",
        name: "about",
        component: () => import("../views/AboutView.vue"),
        meta: {
          title: "about页面",
          // parent: "菜单项1",
        },
      },
      {
        path: "map3d",
        name: "map3d",
        component: () => import("@/views/map/index.vue"),
        meta: {
          title: "mar3d页面",
          children: ["cesium"],
          parent: "map",
        },
        children: [],
      },
      {
        path: "cesium",
        name: "cesium",
        component: () => import("@/views/map/child/child-one.vue"),
        meta: {
          title: "三级菜单2",
          level: "map",
          menuName: '三级菜单标题',
        },
      },
      {
        path: "third-menu",
        name: "third-menu",
        component: () => import("@/views/map/child/child-two.vue"),
        meta: {
          title: "三级菜单1",
          level:'map',
          menuName: '三级菜单标题',
          
        },
      },

      //threejs
      {
        path: "threejs",
        name: "threejs",
        component: () => import("@/views/threeJs/threejs-test.vue"),
        meta: {
          title: "threejs页面",
        },
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/Login.vue"),
  },
  // 404 页面
  {
    path: "/:pathMatch(.*)*",
    name: "notFound",
    component: () => import("../views/NotFoundView.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});
// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!$localStorage.getItem("token"); // 检查是否存在 token

  // 如果目标路由不是登录页，并且没有 token，则重定向到登录页面
  if (to.name !== "login" && !isAuthenticated) {
    next({ name: "login" });
  } else {
    next(); // 继续导航
  }
});

export default router;
